package com.hellorxjava.demo;


import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleEmitter;
import io.reactivex.rxjava3.core.SingleObserver;
import io.reactivex.rxjava3.core.SingleOnSubscribe;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * Single只能发送一个数据或者事件  onSuccess 或者 onError
 * 单个数据生产者
 */
public class RXJavaDemo2 {
    public static void main(String[] args) {
        Single.create(new SingleOnSubscribe<Integer>() {
            @Override
            public void subscribe(@NonNull SingleEmitter<Integer> emitter) throws Throwable {
                if(!emitter.isDisposed()){
//                    emitter.onSuccess(1);
                    emitter.onError(new Throwable("Single error"));
                }
            }
        }).subscribe(new SingleObserver<Integer>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("onSubscribe");
            }

            @Override
            public void onSuccess(@NonNull Integer integer) {
                System.out.println("onSuccess: " + integer);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                System.out.println("onError: " + e.getMessage());
            }
        });
    }
}
